#include "SetupData.h"
#include "ArduinoUtils.h"

SetupData::SetupData(void)
{
}


SetupData::~SetupData(void)
{
}

int SetupData::GetNumberOfConfiguredPins()
{
	return _pins.size();
}

pinData * SetupData::GetPinDataFromIndex(int index)
{
	return _pins[index];
}

pinData * SetupData::GetPinDataFromPinNumber(int pinNumber)
{
	int size = _pins.size();

	for(int i = 0 ; i < size ; ++i)
	{
		if(_pins[i]->pin == pinNumber)
		{
			return _pins[i];
		}
	}

	return NULL;
}

void SetupData::ParseFromString(String setupCommands)
{
	int lastIndex = 0;
	int commandIndex = setupCommands.indexOf("|");

	while(commandIndex != -1)
	{
		pinData * data = new pinData();

		String singleCommand = setupCommands.substring(lastIndex, commandIndex);
		unsigned long numericCommand = singleCommand.toInt();

		data->pin = numericCommand >> 8;
			data->value = 0; // TODO
		
		if((numericCommand & 1) == 1)
		{
			data->type = Input;
		}
		else
		{
			data->type = Output;
		}

		_pins.push_back(data);
		
		lastIndex = commandIndex + 1;
		commandIndex = setupCommands.indexOf("|", lastIndex);
	}
}

vector<commandValues> SetupData::GetAllPinValuesAsCommadValues()
{
	vector<commandValues> values;

	int numberOfPins = _pins.size();

	for(int i = 0 ; i < numberOfPins ; ++i)
	{
		pinData data = *_pins[i];
		commandValues value;
		value.pin = data.pin;		

		switch(data.type)
		{
		case Input:
			value.value = GetSensorValue(data.pin);
			value.command = GET_SENSOR_VALUE;
			break;
		case Output:
			value.value = data.value;
			value.command = SET_OUTPUT_VALUE;
			break;
		}

		values.push_back(value);
	}

	return values;
}